// pages/center/address/show/index.js

Page({

  /**
   * 页面的初始数据
   */
  data: {
    addressInfo: {},
    isDefault: ""
  },
  deleteAddress: function () {
    wx.showModal({
      title: '待完善',
      content: '此功能攻城狮正在努力添加',
      showCancel: false
    })
  },
  setDefault: function () {
    wx.showLoading({
      title: '正在设置默认地址',
    })
    let userID = wx.getStorageSync("cmkc_user_id");
    let addressID = this.data.addressInfo.id
    let ru = getApp().globalData.requestUrl + "5c78f8fccee07";
    let _this = this;
    wx.request({
      url: ru,
      data: { address_id: addressID },
      header: { version: 'v3.0' },
      method: 'POST',
      dataType: 'json',
      responseType: 'text',
      success: function (res) {
        console.log(res);
        _this.updateAddressInfo(addressID);
        wx.hideLoading();
      }
    })
  },
  goHome: function () {
    wx.switchTab({
      url: "/pages/cmmenu/index"
    })
  },
  updateAddressInfo: function (addressID) {
    let ru = getApp().globalData.requestUrl + "5c833429a2445";
    let _this = this;
    wx.request({
      url: ru,
      data: {
        status: "get_address_by_id",
        address_id: addressID
      },
      header: { version: 'v3.0' },
      method: 'POST',
      dataType: 'json',
      responseType: 'text',
      success: function (res) {
        let add = res.data.data.address_info
        let isD = add.is_default ? isD = "是默认地址" : isD = "不是默认地址";
        _this.setData({
          addressInfo: add,
          isDefault: isD
        });
      }
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log("设置默认地址",options);
    this.updateAddressInfo(options.address_id);
  }
})